package cn.wolfcode.shop.controller;

import cn.wolfcode.shop.domain.Catalog;
import cn.wolfcode.shop.service.ICatalogService;
import cn.wolfcode.vo.JSONResultVo;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;

@Controller
@RequestMapping("/catalog")
public class CatalogController {


    /**
     * @Reference 通过dubbo去找该接口下的实现类参数
     */
    @Reference
    private ICatalogService catalogService;

    /**
     * 显示分类的列表界面
     *
     * @param model
     * @return
     */
    @RequestMapping("")
    public String catalogPage(Model model) {

        model.addAttribute("allCatalogJson", catalogService.selectAllByCache());
        return "catalog/catalog";
    }

    /**
     * 显示子分类下的明细
     *
     * @param catalogId
     * @param model
     * @return
     */
    @RequestMapping("/getChildCatalog")
    public String getChildCatalog(Long catalogId, Model model) {

        model.addAttribute("catalogList", catalogService.queryByPid(catalogId));
        return "catalog/child_catalog";
    }

    /**
     * 更新或保存分类信息
     * @param catalog
     * @return
     */
    @RequestMapping("/save")
    @ResponseBody
    public JSONResultVo saveOrUpdate(Catalog catalog) {

        catalogService.saveOrUpdate(catalog);
        return JSONResultVo.instance();
    }

    /**
     * 删除分类信息
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public JSONResultVo delete(Long catalogId){

        catalogService.deleteByCId(catalogId);
        return JSONResultVo.instance();
    }

    /**
     * 更新分类的排序
     * @RequestBody 接收前台的json数据,转成string
     * @param ids
     * @return
     */
    @RequestMapping("/updateSort")
    @ResponseBody
    public JSONResultVo updateSort(@RequestBody List<Long> ids){

        catalogService.batchUpdateSort(ids);
        return JSONResultVo.instance();
    }



}
